package main

import (
	"fmt"

	"gitee.com/Flearning/go_crawl/mgobson"
	"gitee.com/Flearning/go_crawl/xueqiu/userinfo"

	"gopkg.in/mgo.v2/bson"
)

var mgoXueqiuCollection string = "crawl_xueqiu_user"

func main() {
	fmt.Println("用户去重开始...")
	var i, skip int

	mgobson.NewInit()

	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	sigma, _ := c.Find(nil).Count()
	fmt.Println("Σ = ", sigma)
	i = 0
	for {
		skip = i
		clearUser(skip)
		sigma, _ = c.Find(nil).Count()
		fmt.Println("skip -> ", skip, "Σ = ", sigma)
		i++
		if i == sigma {
			break
		}
	}
	sigma, _ = c.Find(nil).Count()
	fmt.Println("Σ = ", sigma)
}

func clearUser(skip int) {
	c := mgobson.MgoSession.DB(mgobson.AuthDatabase).C(mgoXueqiuCollection)
	us := new(userinfo.UserStruct)
	c.Find(nil).Sort("-add_time").Skip(skip).Limit(1).One(us)
	fmt.Println("skip:", skip)
	// fmt.Println(us.AddTime.Unix(), us.Id, us.Id_.Hex())
	//	uWhere := bson.M{
	//		"id": us.Id,
	//	}
	//	q := c.Find(uWhere).Sort("-add_time")
	//	num, _ := q.Count()
	//	fmt.Println(us.Id, num)
	removeW := bson.M{
		"id":       us.Id,
		"add_time": bson.M{"$lt": us.AddTime},
	}
	info, _ := c.RemoveAll(removeW)
	fmt.Println(info.Removed)
}
